﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TestEngine;

namespace TestWindowsGame1
{
    class inGameScene:Scene
    {
        //Actor _actor;

        List<Keys> InputKeys = new List<Keys>();

        Keys[][] Combos = {new Keys[]{Keys.Right, Keys.Right},
                           new Keys[]{Keys.Left, Keys.Right, Keys.B},
                           new Keys[]{Keys.Down, Keys.Right, Keys.A},
                           new Keys[]{Keys.Left, Keys.Down, Keys.Right, Keys.A}};

        Actor _animActor;
        Actor _bActor;
        Actor _cActor;
        InputManager _inputManager;
        SpriteFont _debugFont;
        Utility _utility;
        List<Actor> _actors;
        CollisionManager _cManager;

        public inGameScene(ContentManager content, GraphicsDeviceManager Graphics)
        {
            Content = content;
            _graphics = Graphics;
            _debugFont = Content.Load<SpriteFont>("SpriteFont1");

            _actors = new List<Actor>();

            _animActor = new Actor(new Vector2(115, 200), "Billy");
            _animActor.anim = new Animation(Content.Load<Texture2D>("ws_sSheet"), 6, 5, 30, 10);
            //_animActor.physics = new OrientedPhysics(true);
            _animActor.collider.setCustomCollisionRect(new Vector2(_animActor.anim.ani_Text.Width/6,
                                                                   _animActor.anim.ani_Text.Height/5));
            _animActor.grav = new Vector2(0, 0.002f);
            
            //--TestingSubjects--

            _bActor = new Actor(new Vector2(350, 120), "Mandy");
            _bActor.anim = new Animation(Content.Load<Texture2D>("ws_sSheet"), 6, 5, 30, 10);
            //_bActor.physics = new OrientedPhysics(false);
            _bActor.collider.setCustomCollisionRect(new Vector2(_animActor.anim.ani_Text.Width / 6,
                                                       _animActor.anim.ani_Text.Height / 5));


            _cActor = new Actor(new Vector2(120, 300), "PuroHueso");
            _cActor.anim = new Animation(Content.Load<Texture2D>("ws_sSheet"), 6, 5, 30, 10);
            //_cActor.physics = new OrientedPhysics(false);
            _cActor.collider.setCustomCollisionRect(new Vector2(_animActor.anim.ani_Text.Width / 6,
                                                       _animActor.anim.ani_Text.Height / 5));
            //-------------------

            _actors.Add(_animActor);
            _actors.Add(_bActor);//TS01
            _actors.Add(_cActor);//TS02

            _inputManager = new InputManager(InputKeys, Combos);
            _utility = new Utility(_graphics);
            _cManager = new CollisionManager(_actors);

            InputKeys.Add(Keys.Left);
            InputKeys.Add(Keys.Right);
            InputKeys.Add(Keys.Up);
            InputKeys.Add(Keys.Down);
            InputKeys.Add(Keys.A);
            InputKeys.Add(Keys.B);
            /*_actor = new Actor();
            _actor.tex = Content.Load<Texture2D>("15");
            _actor.position = new Vector2(200, 200);
            _actor.direction = new Vector2(5, -2);*/
        }
        public override void update(int d_time)
        {
            _animActor.update(d_time);
            _inputManager.update(d_time);
            base.update(d_time);

        }
        public override void draw(SpriteBatch sb)
        {
            //sb.Draw(_actor.tex, _actor.position, Color.White);
            sb.DrawString(_debugFont, _inputManager.getBuffer(), Vector2.Zero, Color.White);
            sb.DrawString(_debugFont, _inputManager.lastPressedCombo, new Vector2(0, 200), Color.White);
            sb.Draw(_animActor.anim.ani_Text, _animActor.position, _animActor.anim.getRect(), Color.Orange, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            sb.Draw(_bActor.anim.ani_Text, _bActor.position, _bActor.anim.getRect(), Color.Orange, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            _utility.drawLine(sb, Vector2.Zero, new Vector2(100, 100));
            _utility.drawRect(sb, _animActor.collider.getRect_byVect()[0], _animActor.collider.getRect_byVect()[1]);
            //-----Debugging-----
            _utility.drawRect(sb, _bActor.collider.getRect_byVect()[0], _bActor.collider.getRect_byVect()[1]);
            _utility.drawRect(sb, _cActor.collider.getRect_byVect()[0], _cActor.collider.getRect_byVect()[1]);
            //-------------------
            _utility.drawRect(sb, new Vector2(100, 100), new Vector2(280, 120));//DebuggingDrawRectMethod
            _cManager.drawAll(sb, _debugFont); //---CheckAllCollisionsOnScene---
            base.draw(sb);
        }
        public override void hnd_input()
        {
            if (_inputManager.isNewKeyPress(Keys.Enter))
            {
                SceneManager.scenesToAdd.Add(new inGame3DScene(Content, _graphics));
                readyToExit = true;
            }
            if (_inputManager.isKeyPressed(Keys.Left))
            {
                _animActor.speed += new Vector2(-0.2f, 0);
            }
            if (_inputManager.isKeyPressed(Keys.Right))
            {
                _animActor.speed += new Vector2(0.2f, 0);
            }
            if (_inputManager.isNewMouseClick())
            {
                //_animActor.physics.canJump = false;
                _animActor.accel += new Vector2(0, 0.2f);
            }
            if (_inputManager.isNewMouseRelease())
            {
                //_animActor.physics.canJump = true;
            }

            base.hnd_input();
        }
    }
}
